package com.situ.mall.controller.front;

import java.util.List;

import com.situ.mall.pojo.entity.User;
import com.situ.mall.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.situ.mall.pojo.entity.Category;
import com.situ.mall.service.ICategoryService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class FrontIndexController {
	@Autowired
	private ICategoryService categoryService;

	// 商城主页
	@RequestMapping("/index.shtml")
	public String frontIndex(Model model) {
		List<Category> topCategoryList = categoryService.selectTopCategory();
		List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
		model.addAttribute("topCategoryList", topCategoryList);
		model.addAttribute("secondCategoryList", secondCategoryList);
		return "front/index";
	}

	// 判断是否登录（session中是否有用户），对应显示页面顶部的登录显示
	@ResponseBody
	@RequestMapping("/indexIsLogin.shtml")
	public JSONResult indexIsLogin(HttpSession session) {
		JSONResult jsonResult = null;
		User user = (User) session.getAttribute("user");
		if (user == null) {
			jsonResult = JSONResult.error("未登录");
		} else {
			jsonResult = JSONResult.success("已登录", user.getUsername());
		}
		return jsonResult;
	}
}
